package com.lrkj.common.utils;

import com.google.common.base.Joiner;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtils {


    public static Map<String, Object>  transformMapDataType(Map<String, Object> map) {
        Map<String, Object> resultMap = new HashMap<>();

        if (null != map) {
            map.forEach((k, v) -> {
                if (null != v && v instanceof java.sql.Timestamp) {
                    resultMap.put(k, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, (Date) v));
                } else {
                    resultMap.put(k, v);
                }
            });

        }
        return resultMap;

    }


    public static List<Map<String, Object>> transformListMapDataType(List<Map<String, Object>> maps) {
        List<Map<String, Object>> resultListMap = new ArrayList<>();
        if (null != maps) {
            maps.stream().forEach(map->{
                Map<String, Object> resultMap = new HashMap<>();
                map.forEach((k, v) -> {
                    if (null != v && v instanceof java.sql.Timestamp) {
                        resultMap.put(k, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, (Date) v));
                    } else {
                        resultMap.put(k, v);
                    }
                });
                resultListMap.add(resultMap);
            });
        }
        return resultListMap;

    }

    /**
     * 驼峰转下划线
     */
    public static String humpToLine(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 下划线转驼峰
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Pattern linePattern = Pattern.compile("_(\\w)");
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String handleInParam(String src) {
        List<String> result = new ArrayList<>();
        if (StringUtils.isNotEmpty(src)) {
            String[] values = src.split(",");
            for (int i = 0; i < values.length; i++) {
                result.add("'" + values[i] + "'");
            }
        }
        return StringUtils.join(result, ",");
    }


    public static String asUrlParams(String url,Map<String, String> source){
        String param = Joiner.on("&")
                // 用指定符号代替空值,key 或者value 为null都会被替换
                .useForNull("")
                .withKeyValueSeparator("=")
                .join(source);
        return url + "?" + param;
    }

}
